package samples.java.json.gson;

import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wang.yuchuan
 * @date 2018/1/31
 */
@Slf4j
public class PrimitiveTypeGsonSamples {


    public void serialize() {
        // Serialization
        Gson gson = new Gson();
        String intGson = gson.toJson(1);
        String strGson = gson.toJson("abcd");// ==> "abcd"
        String longGson = gson.toJson(new Long(10));// ==> 10
        int[] values = {1};
        String intArrGson = gson.toJson(values);// ==> [1]

        log.info("intGson: {} strGson:{} longGson:{} intArrGson:{}", intGson, strGson, longGson, intArrGson);
    }

    public void deserialize() {
        // Deserialization
        Gson gson = new Gson();
        int one = gson.fromJson("1", int.class);
        Integer one2 = gson.fromJson("1", Integer.class);
        Long one3 = gson.fromJson("1", Long.class);
        Boolean boolFalse = gson.fromJson("false", Boolean.class);
        String str = gson.fromJson("\"abc\"", String.class);
        String[] anotherStr = gson.fromJson("[\"abc\"]", String[].class);

        log.info("deserialize int:{} Integer:{} Long:{} boolean:{} str:{} anotherStr:{}", one, one2, one3, boolFalse, str, anotherStr);
    }
}
